/*
 * JaLingo, http://jalingo.sourceforge.net/
 *
 * Copyright (c) 2002-2006 Oleksandr Shyshko
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package uwncorrector.client.application.model;

import uwncorrector.centre.util.io.lock.LockedException;
import uwncorrector.centre.util.measurer.TimeMeasurer;
import uwncorrector.client.application.gui.actions.Actions;
import uwncorrector.client.application.gui.drophandler.DropHandler;
import uwncorrector.client.application.gui.main.MainGui;
import uwncorrector.client.application.gui.main.MainPanel;
import uwncorrector.client.application.gui.main.describer.DescriberGui;
import uwncorrector.client.application.gui.main.export.ExportGui;
import uwncorrector.client.application.gui.main.help.HelpGui;
import uwncorrector.client.application.gui.main.history.HistoryGui;
import uwncorrector.client.application.gui.main.navigator.NavigatorGui;
import uwncorrector.client.application.gui.main.popout.PopOut;
import uwncorrector.client.application.gui.main.search.SearchGui;
import uwncorrector.client.application.gui.main.settings.SettingsGui;
import uwncorrector.client.application.gui.main.settings.appearance.AppearanceGui;
import uwncorrector.client.application.gui.main.settings.behavior.BehaviorGui;
import uwncorrector.client.application.gui.main.settings.database.DatabaseGui;
import uwncorrector.client.application.gui.main.settings.dictionaries.DictionariesGui;
import uwncorrector.client.application.gui.main.settings.dictionaries.add.AddGui;
import uwncorrector.client.application.gui.main.suggest.SuggestGui;
import uwncorrector.client.application.gui.main.synset.SynsetGui;
import uwncorrector.client.engine.Engine;
import uwncorrector.client.engine.IEngine;
import uwncorrector.client.engine.UnknownCacheVersionException;
import uwncorrector.client.synset.BadSynsetManager;
import uwncorrector.client.synset.POS;
import uwncorrector.client.synset.Word;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.IOException;
import java.util.List;

public class Skeleton {
    private static final Log LOG = LogFactory.getLog(Skeleton.class);

    private Model model;

    public Skeleton(Preferences preferences) throws LockedException, UnknownCacheVersionException, IOException {
        LOG.info( "Initializing engine..." );
        TimeMeasurer measurer = new TimeMeasurer();
        IEngine engine = new Engine();
        LOG.info( "Initializing engine: done within " + measurer );

        LOG.info( "Initializing UI: model and history..." );
        model = new Model(engine, preferences);

        History history = new History();

        LOG.info( "Initializing UI..." );
        measurer = new TimeMeasurer();

        LOG.info( "Initializing UI: actions..." );
        Actions actions = new Actions(model, history);

        DropHandler dropHandler = new DropHandler(engine, model);

        LOG.info( "Initializing UI: navigator..." );
        NavigatorGui navigatorGui = new NavigatorGui(model, engine, dropHandler);
        SearchGui searchGui = new SearchGui(model, engine);
        HistoryGui historyGui = new HistoryGui(model, engine, history);

        LOG.info( "Initializing UI: synset..." );
        SynsetGui synsetGui = new SynsetGui(model);
        LOG.info( "Initializing UI: describer..." );
        DescriberGui describerGui = new DescriberGui(model, actions, engine, dropHandler);

        // main
        LOG.info( "Initializing UI: main..." );
        MainPanel mainPanel = new MainPanel(model, engine, actions, history, navigatorGui, historyGui, searchGui, synsetGui, describerGui);
        MainGui mainGui = new MainGui(model, mainPanel);

        // settings
        LOG.info( "Initializing UI: settings..." );
        //DatabaseGui databaseGui = new DatabaseGui(model);
        DictionariesGui dictionariesGui = new DictionariesGui(model, engine);
        AppearanceGui appearanceGui = new AppearanceGui(model);
        BehaviorGui behaviorGui = new BehaviorGui(model);
        SettingsGui settingsGui = new SettingsGui(actions, model, engine, mainGui.getFrame(), dictionariesGui, appearanceGui, null, behaviorGui);

        new AddGui(model, engine, settingsGui.getDialog());

        // other
        LOG.info("Initializing UI: other...");
        new HelpGui(model, mainGui.getFrame());
        //new ExportGui( engine, model, mainGui.getFrame() );

        //new DropZoneGui( model, actions, dropHandler );

        new SuggestGui(model, engine, describerGui.getGui());

        new PopOut(model, engine, describerGui.getGui());
        LOG.info("Initializing UI: done within " + measurer);

        LOG.info("Updating UI...");
        measurer = new TimeMeasurer();
        start();
        LOG.info("Updating UI: done within " + measurer);
    }

    private void start() {
        model.intialize();
        model.settingsUpdated();
    }

    public void show() {
        model.main_showAtTop();
    }

    public void after_show() {
        model.load_finished();
    }
}
